﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.Domain.Core.Entities;
using Cesave.Domain.PermisosModule.Contracts.DistritosPersonal;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.IoC;
using Cesave.Domain.PermisosModule.Contracts.Ciclos;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Distritos
{
	public partial class DistritosViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private DistritoPersonal _distritoPersonal;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public DistritoPersonal DistritoPersonal
		{
			get
			{
				return _distritoPersonal;
			}
			private set
			{
				_distritoPersonal = value;
				if (_distritoPersonal != null)
					RaisePropertyChanged("DistritoPersonal");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsDistritoPersonalReady
		{
			get
			{
				return DistritoPersonal != null;
			}
		}

		#endregion

		#region Actions

		/// <summary>
		/// 
		/// </summary>
		private void LoadDistritoPersonal()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					if (!IsCicloReady)
					{
						ICicloService cicloService = IoCFactory.Resolve<ICicloService>();
						Ciclo = cicloService.FindCicloActual();
					}
					IDistritoPersonalService distritoPersonalService = IoCFactory.Resolve<IDistritoPersonalService>();
					e.Result = distritoPersonalService.FindDistritoPersonalByDistritoAndCiclo(Distrito.DistritoGuid, Ciclo.CicloGuid);
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
					{
						DistritoPersonal = e.Result as DistritoPersonal;
					}
					else if (e.Error != null)
						MessageBox.Show(
							_distritosView,
							e.Error.Message,
							WindowTitles.DistritosWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveDistritoPersonal(object sender, EventArgs e)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (wSender, wArgs) =>
				{
					IDistritoPersonalService distritoPersonalService = IoCFactory.Resolve<IDistritoPersonalService>();
					distritoPersonalService.ChangeDistritoPersonal(DistritoPersonal);
				};
				worker.RunWorkerCompleted += (wSender, wArgs) =>
				{
					EndWaiting();
					if (!wArgs.Cancelled && wArgs.Error == null)
						LoadDistritos();
					else if (wArgs.Error != null)
						MessageBox.Show(
							_distritosView,
							wArgs.Error.Message,
							WindowTitles.DistritosWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DeleteDistritoPersonal(object sender, EventArgs e)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (wSender, wArgs) =>
				{
					IDistritoPersonalService distritoPersonalService = IoCFactory.Resolve<IDistritoPersonalService>();
					DistritoPersonal.MarkAsDeleted();
					distritoPersonalService.DeleteDistritoPersonal(DistritoPersonal);
				};
				worker.RunWorkerCompleted += (wSender, wArgs) =>
				{
					EndWaiting();
					if (!wArgs.Cancelled && wArgs.Error == null)
					{
						_distritosView.distritosBindingSource.Clear();
					}
					else if (wArgs.Error != null)
						MessageBox.Show(
							_distritosView,
							wArgs.Error.Message,
							WindowTitles.DistritosWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddNewDistritoPersonal(object sender, EventArgs e)
		{
			_distritosView.distritosPersonalBindingSource.AddingNew += AddingNewDistritoPersonal;
			_distritosView.distritosPersonalBindingSource.AddNew();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddingNewDistritoPersonal(object sender, AddingNewEventArgs e)
		{
			IDistritoPersonalService distritoPersonalService = IoCFactory.Resolve<IDistritoPersonalService>();
			DistritoPersonal = distritoPersonalService.FindDistritoPersonalByDistritoAndCiclo(Distrito.DistritoGuid, Ciclo.CicloGuid);
			e.NewObject = DistritoPersonal;
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentDistritoPersonal(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null)
				if (bindingSource.Current != null)
					DistritoPersonal = bindingSource.Current as DistritoPersonal;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnDistritoPersonalPropertyChanged()
		{
			_distritosView.distritosPersonalBindingSource.CurrentChanged += SetCurrentDistritoPersonal;
			_distritosView.distritosPersonalBindingSource.DataSource = DistritoPersonal;
		}

		#endregion
	}
}
